﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Credisoft.Models;
using Credisoft.Models.Credisoft_BO;
using Credisoft.Models.Credisoft_DAO;

namespace Credisoft.Controllers.Maintenance
{
    public class EnterpriseController : Controller
    {
        //
        // GET: /Enterprise/

        public ActionResult SearchEnterprise()
        {
            return View();
        }

        public ActionResult AddEnterprise()
        {
            return View();
        }

        public ActionResult EditEnterprise(String id)
        {

            ViewBag.enterpriseId = id;
            return View();
        }

        public ActionResult ViewEnterprise(String id)
        {
            ViewBag.enterpriseId= id;

            return View();
        }

        [HttpPost]
        public JsonResult getEnterpriseList()
        {
            List<EnterpriseBO> listEnterprise = new List<EnterpriseBO>();

            try
            {
                // Se carga la lista de empresas registradas en el sistema.
                listEnterprise = new EnterpriseDAO().getEnterpriseDataList();

                // Se definen estructuras de datos auxiliares
                SectorBO sectorFound;
                EconomicGroupBO economicGroupFound;
                PersonBO personFound;

                // Se obtiene por cada empresa registrada los datos de su representante legal, sector economico y grupo economico
                foreach (EnterpriseBO enterpriseAux in listEnterprise)
                {
                    sectorFound = new SectorDAO().getSectorDataById(enterpriseAux.sectorId);
                    economicGroupFound = new EconomicGroupDAO().getEconomicGroupDataById(enterpriseAux.groupId);
                    personFound = new PersonDAO().getPersonDataById(enterpriseAux.agentId);

                    enterpriseAux.sectorName = sectorFound.name;
                    enterpriseAux.economicGroupName = economicGroupFound.name;
                    enterpriseAux.agentName = personFound.names + " " + personFound.firstName + " " + personFound.lastName;
                    enterpriseAux.agentDNI = personFound.dni;
                    enterpriseAux.agentEmail = personFound.email;
                    enterpriseAux.agentPhone = personFound.phonenumber;
                }
            }
            catch (Exception e)
            {
                listEnterprise = null;
            }

            return new JsonResult() { Data = listEnterprise };
        }

        [HttpPost]
        public JsonResult insertEnterprise(EnterpriseBO enterprise)
        {
            // Data Hardcodeada
            /*enterprise.agentName = "Harold";
            enterprise.agentFirstName = "Marchena";
            enterprise.agentLastName = "Soto";
            enterprise.agentDNI = "75435345";
            enterprise.agentEmail = "h.soto@bcp.com.pe";
            enterprise.agentPhone = "9863445";

            enterprise.socialReason = "BCP";
            enterprise.ruc = "1235646546";
            enterprise.recordDate = DateTime.Now;
            enterprise.sectorId = 1;
            enterprise.groupId = 2;
            enterprise.agentId = 2;
            enterprise.creationDate = DateTime.Now;
            enterprise.officialAddress = "Calle Centenario 189";
            enterprise.cem = 0;
            enterprise.defaultState = 0;
            enterprise.phone = "3575756";*/

            int success = 0;
            persona personDE = new persona();

            personDE.nombres = enterprise.agentName;
            personDE.apellidopaterno = enterprise.agentFirstName;
            personDE.apellidomaterno = enterprise.agentLastName;
            personDE.dni = enterprise.agentDNI;
            personDE.email = enterprise.agentEmail;
            personDE.telefono = enterprise.agentPhone;
            personDE.direccion = "";

            success = new PersonDAO().insertPerson(personDE);

            if (success == 1)
            {
                success = 0;
                empresa enterpriseDE = new empresa();

                enterpriseDE.razonsocial = enterprise.socialReason;
                enterpriseDE.ruc = enterprise.ruc;
                enterpriseDE.fecharegistro = DateTime.Now;
                enterpriseDE.idsector = enterprise.sectorId;
                enterpriseDE.idgrupo = enterprise.groupId;
                enterpriseDE.idrepresentante = personDE.idpersona;
                enterpriseDE.fechacreacion = DateTime.Now;
                enterpriseDE.direccionfiscal = enterprise.officialAddress;
                enterpriseDE.cem = (decimal) enterprise.cem;
                enterpriseDE.estadomorosidad = (short) enterprise.defaultState;
                enterpriseDE.telefono = "";

                success = new EnterpriseDAO().insertEnterprise(enterpriseDE);
            }
			
            return new JsonResult() { Data = enterprise };
        }

        [HttpPost]
        public JsonResult editEnterprise(EnterpriseBO enterprise)
        {
            PersonBO personAux = new PersonBO();

            personAux.personId = enterprise.agentId;
            personAux.names = enterprise.agentName;
            personAux.firstName = enterprise.agentFirstName;
            personAux.lastName = enterprise.agentLastName;
            personAux.dni = enterprise.agentDNI;
            personAux.email = enterprise.agentEmail;
            personAux.phonenumber = enterprise.agentPhone;
            personAux.address = "";

            new PersonDAO().editPerson(personAux);
            new EnterpriseDAO().editEnterprise(enterprise);

            return new JsonResult() { Data = enterprise };
        }

        [HttpPost]
        public JsonResult getEnterprise(EnterpriseBO enterprise)
        {

            EnterpriseBO enterpriseAux = new EnterpriseBO();

            enterpriseAux = new EnterpriseDAO().getEnterpriseDataById(enterprise.enterpriseId);
               

            SectorBO sectorFound;
            EconomicGroupBO economicGroupFound;
            PersonBO personFound;

            sectorFound = new SectorDAO().getSectorDataById(enterpriseAux.sectorId);
            economicGroupFound = new EconomicGroupDAO().getEconomicGroupDataById(enterpriseAux.groupId);
            personFound = new PersonDAO().getPersonDataById(enterpriseAux.agentId);

            enterpriseAux.sectorName = sectorFound.name;
            enterpriseAux.economicGroupName = economicGroupFound.name;
            enterpriseAux.agentName = personFound.names;
            enterpriseAux.agentFirstName = personFound.firstName;
            enterpriseAux.agentLastName = personFound.lastName;
            enterpriseAux.agentDNI = personFound.dni;
            enterpriseAux.agentEmail = personFound.email;
            enterpriseAux.agentPhone = personFound.phonenumber;

            return new JsonResult() { Data = enterpriseAux };
        }

        [HttpPost]
        public JsonResult getSectorList()
        {
            List<SectorBO> listSector = new List<SectorBO>();

            try
            {
                // Se carga la lista de sectores registradas en el sistema.
                listSector = new SectorDAO().getSectorDataList();

           
            }
            catch (Exception e)
            {
                listSector = null;
            }

            return new JsonResult() { Data = listSector };
        }

        [HttpPost]
        public JsonResult getEconomicGroupList()
        {
            List<EconomicGroupBO> listEconomicGroup = new List<EconomicGroupBO>();

            try
            {
                // Se carga la lista de sectores registradas en el sistema.
                listEconomicGroup = new EconomicGroupDAO().getEconomicGroupDataList();


            }
            catch (Exception e)
            {
                listEconomicGroup = null;
            }

            return new JsonResult() { Data = listEconomicGroup };
        }

        [HttpPost]
        public JsonResult insertBusinessGroup(EconomicGroupBO economicGroup)
        {
            int success = 0;
            grupoeconomico economicGroupDE = new grupoeconomico();

            economicGroupDE.nombre = economicGroup.name;
            economicGroupDE.credibilidad = economicGroup.credibility;

            success = new EconomicGroupDAO().insertEconomicGroup(economicGroupDE);

            return new JsonResult() { Data = economicGroup };
        }
    }

    
}
